﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MathQuizer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MultiplierWindow : Window
    {

        System.Random _myRandoms = new Random();
        
        System.Windows.Threading.DispatcherTimer _timer;
            //_newProblemDelayTimer = new System.Timers.Timer(3000);
        /// <summary>
        /// Creates the problem.
        /// </summary>
        void CreateProblem()
        {

            
            _multiplierOne.Text = _myRandoms.Next(0, 12).ToString();
            _multiplierTwo.Text = _myRandoms.Next(0, 12).ToString();
            _resultTextBox.Text = "";
            _resultTextBox.Background = Brushes.White;

        }

        /// <summary>
        /// Checks the problem.
        /// </summary>
        /// <param name="multiplier1">The multiplier1.</param>
        /// <param name="multiplier2">The multiplier2.</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        bool CheckProblem(int multiplier1, int multiplier2, int result)
        {
            return ((multiplier1 * multiplier2) == result);

        }

        /// <summary>
        /// Sets the result in error.
        /// </summary>
        void SetResultInError()
        {
            _resultTextBox.Background = Brushes.LightPink;
        }

        /// <summary>
        /// Sets the result OK.
        /// </summary>
        void SetResultOK()
        {
            _resultTextBox.Background = Brushes.LightGreen;
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="Window1"/> class.
        /// </summary>
        public MultiplierWindow()
        {
            InitializeComponent();
            CreateProblem();
            _timer = new System.Windows.Threading.DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += new EventHandler(_timer_Tick);
                
            
            
        }

        /// <summary>
        /// Handles the Tick event of the _timer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void _timer_Tick(object sender, EventArgs e)
        {
            CreateProblem();
            _timer.Stop();
        }

        /// <summary>
        /// Handles the Click event of the _newProblemButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void _newProblemButton_Click(object sender, RoutedEventArgs e)
        {
            CreateProblem();
        }


        /// <summary>
        /// Handles the KeyDown event of the _resultTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void _resultTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (CheckProblem(Convert.ToInt32(_multiplierOne.Text), Convert.ToInt32(_multiplierTwo.Text), Convert.ToInt32(_resultTextBox.Text)))
                    {
                        SetResultOK();
                        _timer.Start();
                    }
                    else
                    {
                        SetResultInError();
                    }
                }
            }
            catch (System.FormatException)
            {
                SetResultInError();
            }

        }
    }
}
